import axios from '@/utils/request'

// 查询字典数据列表
export function listData (query) {
  return axios({
    url: '/system/dict/data/list',
    method: 'get',
    params: query
  })
}

// 查询字典数据详细
export function getData (dictCode) {
  return axios({
    url: '/system/dict/data/' + dictCode,
    method: 'get'
  })
}

// 根据字典类型查询字典数据信息
export function getDicts (dictType) {
  return axios({
    url: '/system/dict/data/type/' + dictType,
    method: 'get'
  })
}

/**
 * 根据类型获取字典内容 返回Map
 * @param {String} dictType 字典类型
 */
export function getDictMap (dictType) {
  return new Promise((resolve, reject) => {
  // ajax
    axios({
      url: '/system/dict/data/type/' + dictType,
      method: 'get'
    }).then(res => {
      const map = new Map()
      res.map(d => {
        map.set(d.dictValue, d.dictLabel)
      })
      resolve(map)
    }).catch(err => {
      reject(err)
    })
  })
}

// 新增字典数据
export function addData (data) {
  return axios({
    url: '/system/dict/data',
    method: 'post',
    data: data
  })
}

// 修改字典数据
export function updateData (data) {
  return axios({
    url: '/system/dict/data',
    method: 'put',
    data: data
  })
}

export function saveDictData (parameter) {
  return axios({
    url: '/system/dict/data',
    method: parameter.dictCode > 0 ? 'put' : 'post',
    data: parameter
  })
}

// 删除字典数据
export function delData (dictCode) {
  return axios({
    url: '/system/dict/data/' + dictCode,
    method: 'delete'
  })
}

// 导出字典数据
export function exportData (query) {
  return axios({
    url: '/system/dict/data/export',
    method: 'get',
    params: query
  })
}
